/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import java.sql.*;
import Database.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import java.security.*;
import java.util.Vector;
import javax.crypto.*;
import sun.misc.*;

/**
 *
 * @author Jack
 */
public class CommonQuery {

    public static boolean Command(String query) {
        boolean kq = true;

        try {
            Connection con = ConnectToDatabase.getConnection();

            Statement st = con.createStatement();
            kq = st.executeUpdate(query) > 0;
        } catch (Exception ex) {
            kq = false;
            //        JOptionPane.showMessageDialog(null, ex.toString());
        }

        return kq;
    }

    public static boolean CheckString(String a) {
        String b = a.trim();
        try {
            if (b.length() == 0) {
                return false;
            }
        } catch (Exception e) {
            return false;

        }
        return true;
    }

    public static boolean CheckIntNumber(String a) {
        try {
            int so = Integer.valueOf(a);
            if (a == null || so < 0) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static boolean CheckDoubleNumber(String a) {
        try {
            double so = Double.valueOf(a);
            if (a == null || so <= 0) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static boolean CheckEmail(String email) {
        String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public static boolean CheckPhone(String Phone) {
        String PHONE_PATTERN = "[_0-9-]{10,11}";
        Pattern pattern = Pattern.compile(PHONE_PATTERN);
        Matcher matcher = pattern.matcher(Phone);
        return matcher.matches();
    }

    public boolean CheckDate(String inDate, String patter) {

        if (inDate == null) {
            return false;
        }

        //set the format to use as a constructor argument
        SimpleDateFormat dateFormat = new SimpleDateFormat(patter);

        if (inDate.trim().length() != dateFormat.toPattern().length()) {
            return false;
        }

        dateFormat.setLenient(false);

        try {
            //parse the inDate parameter
            dateFormat.parse(inDate.trim());
        } catch (ParseException pe) {
            return false;
        }
        return true;
    }
    /*-------ma hoa*/
    private SecretKey skey;

    public void GeneteKey() {
        try {
            KeyGenerator kq = KeyGenerator.getInstance("DES");
            kq.init(new SecureRandom());
            skey = kq.generateKey();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public String Encrypt(String message, int ide) {

        String result = "";
        try {
            // toa khoa cho viec ma hoa
            this.GeneteKey();

            Cipher ci = Cipher.getInstance("DES");
            ci.init(Cipher.ENCRYPT_MODE, skey);
            FileOutputStream fos = new FileOutputStream("src/bugreportingsystem/resources/SecretKey/skey" + ide);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(skey);
            oos.close();
            fos.close();
            // ma hoa mang bye cua noi dung
            byte[] bs = message.getBytes();
            byte[] nbs = ci.doFinal(bs);

            //chuyen magng bye thanh chuoi
            BASE64Encoder encode = new BASE64Encoder();
            result = encode.encode(nbs);

        } catch (Exception e) {
            System.out.println(e);
        }
        return result;
    }

    public String DEncrypt(String message, int ide) {
        String result = "";
        try {
            FileInputStream fin = new FileInputStream("src/bugreportingsystem/resources/SecretKey/skey" + ide);
            ObjectInputStream ois = new ObjectInputStream(fin);
            skey = (SecretKey) ois.readObject();
            ois.close();
            fin.close();
            Cipher ci = Cipher.getInstance("DES");
            ci.init(Cipher.DECRYPT_MODE, skey);

            BASE64Decoder dcode = new BASE64Decoder();
            byte[] bs = dcode.decodeBuffer(message);
            byte[] nbs = ci.doFinal(bs);
            result = new String(nbs);
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("khong giai ma dc");
        }
        return result;
    }
}
